<template>
  <div>
    <el-form
      ref="query"
      :model="query"
      :inline="true"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="图书编号">
        <el-input v-model="query.book_num" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="query.book_name" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="query.writer" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="所在位置">
        <el-input v-model="query.site" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="借阅时间">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="query.lend_time"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="归还时间">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="query.revert_time"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-button
        @click="bookQuery"
        size="mini"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button @click="resetForm" size="mini" icon="el-icon-refresh"
        >重置</el-button
      >
    </el-form>

    <!-- <div class="btn">
      <div class="right">
        <el-button
          @click="bookQuery"
          size="mini"
          type="primary"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-button @click="resetForm" size="mini" icon="el-icon-refresh"
          >重置</el-button
        >
      </div>
    </div> -->

    <el-table :data="tableData" border size="mini">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="book_num" label="图书编号" width="180">
      </el-table-column>
      <el-table-column prop="book.book_name" label="图书名称" width="140">
      </el-table-column>
      <el-table-column prop="book.writer" label="作者" width="90">
      </el-table-column>
      <el-table-column prop="book.site" label="位置" width="90">
      </el-table-column>
      <el-table-column prop="lend_time" label="借阅时间" width="150">
      </el-table-column>
      <el-table-column prop="revert_time" label="预计归还时间" width="100">
      </el-table-column>
      <el-table-column prop="real_time" label="归还时间" width="150">
      </el-table-column>
      <el-table-column prop="state" label="是否归还" width="95">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.state == 1">已归还</el-tag>
          <el-tag type="danger" v-if="scope.row.state == 0">未归还</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="95">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="success"
            plain
            @click="revertBook(scope.$index, scope.row)"
            >还书</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDataTotal"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { page, revertBook as revertBook1 } from "../../api/book/borrow";

export default {
  data() {
    return {
      tableData: [],
      query: { page: 1, limit: 10 },
      tableDataTotal: 0,
      //   isHaveAmount: false,
      //   bookName: "",
      //   borrowTimeVisible: false,
      //   borrowForm: {},
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    indexMethod(index) {
      return (this.query.page - 1) * this.query.limit + (index + 1);
    },
    getPage() {
      page(this.query).then((res) => {
        console.log(res);
        this.tableData = res.data.rows;
        this.tableDataTotal = res.data.count;
      });
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.getPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getPage();
    },
    bookQuery() {
      this.getPage();
    },
    resetForm() {
      this.query = { page: 1, limit: 10 };
      this.getPage();
    },
    revertBook(index, row) {
      if (row.state == '1' ) {
        this.$message.warning(`【${row.book.book_name}】这本书已经归还！`);
      } else {
        this.$confirm(
          `确定归还【${row.book.book_name}】这本书, 是否继续?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            revertBook1(row).then((res) => {
              if (res.success) {
                this.$message({ type: "success", message: "归还成功!" });
                this.getPage();
              } else {
                this.$message.error(res.data);
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消归还",
            });
          });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>